package view;

import java.io.FileWriter;
import javax.swing.JOptionPane;
import model.cubos.CuboMagico;
import model.cubos.CuboMagico3x3x3;
import model.rotations.RotationsImpl;
import model.strategys.AlgoritmoBasico;

public class MainAuxiliar {

    private static String[] configuracoesTestadas = {
        "Scramble:  U  D  L  D'  F'  D'  L  F  B2  D  U'  F2  U'  F'  B  D'  R'  B2  L'  U'  L2  D2  B  R'  F",
        "Scramble:  B'  U2  D  B2  F2  U2  D  R2  B2  F'  U2  R'  D  L'  D2  B'  L2  F2  B2  L  F'  U'  R  U2  D",
        "Scramble:  B  L'  R'  D2  R'  U  B'  L2  U  L'  R'  F2  B2  L'  U2  D'  R  B  F'  D2  U'  F  B'  U'  R2",
        "Scramble:  R2  F  L'  R'  B'  U  B2  D2  F2  D2  R  U2  F2  R  U2  F  U2  B2  R2  D  U'  F  L  U2  F'",
        "Scramble:  L  D  B  D'  F  R'  F2  D2  B  R  F'  R2  B  U  R2  U2  L2  F'  L2  D2  R2  D'  L'  R  U2",
        "Scramble:  U2  R'  B'  R2  U'  F2  D  R'  L2  B'  F2  R'  U  R  B  D2  R'  F'  U  R'  L  D'  B  F'  L2",
        "Scramble:  U2  L  F2  D'  B'  U2  F2  L'  B  U  F  D  B'  D  B2  F2  D2  U'  L'  D  U  R  B  L2  U",
        "Scramble:  U  B2  F  U2  R  L'  F'  U2  L  F'  R'  F  U  B2  D  R2  D2  F  U'  B  D'  R  L'  U  B2",
        "Scramble:  L2  D2  R  B2  R2  L'  D2  U2  B  U2  B  L'  U'  D  B2  U'  B'  F2  U2  L  F2  U2  B2  F2  L'",
        "Scramble:  F2  U  F2  D'  U2  F2  D2  F  B'  L'  R  F'  L  F'  U'  L'  R  U2  F'  R'  B  F2  L'  R2  F2",
        "Scramble:  U  F'  U'  R'  U  D  L  U'  F2  B2  D  B  L'  U'  F  D'  R'  F2  U2  L  D  L  F  L2  F2",
        "Scramble:  R'  U'  D'  F2  R  D'  L  R2  F'  L  B'  R2  D  F'  U'  D  B'  R  L'  B'  L  U2  B2  F  R'",
        "Scramble:  R2  B'  D2  L  R2  D  R2  F'  R  L'  F  R  F  R2  L'  F'  D'  R'  B'  R  F'  B2  R  F2  U'",
        "Scramble:  R2  B'  L2  R  D2  R2  B2  R2  U  F'  R'  F'  L'  B'  F2  L  B'  F'  D'  U'  R  F'  D  U2  B2",
        "Scramble:  D  L  B  D2  L'  R  U2  L  R2  U2  L2  U'  R'  B2  D  B2  D  F  U'  B'  L2  B  R'  F2  D'",
        "Scramble:  D2  L  D2  B2  L2  F2  U  D  B2  F'  L2  D2  B  U'  D  R'  L'  F  B  R  U'  B2  F'  L  R2",
        "Scramble:  B2  D2  L'  B2  R  F2  U2  L'  D'  R  L2  U  D2  B  L'  R'  U2  L2  R'  D'  L'  R2  F2  R  U2",
        "Scramble:  R  F'  R'  D  R2  U'  B  R  B2  R2  B'  L2  F2  D  L  R'  U2  R'  L2  F2  L2  F2  L'  U'  R'",
        "Scramble:  B  L'  D'  L'  D2  R'  U  R'  B  L  U2  L2  B2  F  D'  R2  B'  U'  L2  D  B  L'  D  L  U2",
        "Scramble:  B  U  F'  R  D2  L2  R  D'  F2  U'  F2  L  U  L'  F2  B2  L'  R'  F'  U'  D2  B'  L2  R'  F2"
    };
    public final static String pasta = "./";
    public final static String arquivo_embaralhados = pasta + "cubo_embaralhados";
    public final static String arquivo_resolvidos = pasta + "cubo_resolvidos";
    public final static String arquivo_passo_anterior = pasta + "cubo_passo_anterior";

    /**
     * @param args
     */
    public static void main(String[] args) {
        long inicial = System.currentTimeMillis();
        CuboMagico3x3x3 cubo = new CuboMagico3x3x3(new AlgoritmoBasico());
        ((AlgoritmoBasico) cubo.getStrategy()).setCubo(cubo);
        RotationsImpl rotacao = new RotationsImpl();
        gravar(cubo);
        for (int i = 0; i < configuracoesTestadas.length; i++) {
            rotacao.executa(cubo, spliter(i));
            Gravar(arquivo_embaralhados, "Cubo " + i + " embaralhado:");
            gravar(cubo);
            Gravar(arquivo_resolvidos, "Cubo " + i + " resolvido:");
            cubo.resolver();
            gravar(arquivo_resolvidos, cubo);
        }
        long the_final = System.currentTimeMillis();
        long tempo = ((the_final - inicial) / 1000);
        gravar("Fim.   Terminou em: " + (tempo / 60) + "min e " + (tempo % 60) + "s.");
        JOptionPane.showMessageDialog(null, "Cubos resolvidos com sucesso", "Concluído", JOptionPane.DEFAULT_OPTION);


    }

    public static void gravar(String texto) {
        Gravar(arquivo_embaralhados, texto);
    }

    public static void gravar(CuboMagico3x3x3 cubo) {
        gravar(arquivo_embaralhados, cubo);
    }

    public static void gravar(String local, CuboMagico3x3x3 cubo) {
        Gravar(local, "Cubo em números: ");
        cubo.setToStringToNumbers();
        Gravar(local, cubo.toString());
        Gravar(local, "Cubo em cores: ");
        cubo.setToStringToColor();
        Gravar(local, cubo.toString());
    }

    public static String[] spliter(int i) {
        return spliter(configuracoesTestadas[i]);
    }

    public static String[] spliter(String string) {
        string = string.replace("Scramble:  ", "");
        string = string.replace("'", "_");
        return (string.split("  "));
    }

    public static void doublePrint(CuboMagico cubo) {
        cubo.setToStringToNumbers();
        System.out.println(cubo);
        cubo.setToStringToColor();
        System.out.println(cubo);
    }

    public static void Gravar(String local, String texto) {
        String conteudo = texto;
        try {
            try (FileWriter x = new FileWriter(local, true)) {
                conteudo += "\n\r"; // criando nova linha e recuo no arquivo           
                x.write(conteudo);
            }
        } // em caso de erro apreenta mensagem abaixo  
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Atenção", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }
    }
}